﻿using System.Collections.Generic;
using System.Linq;
using Myotragus.Data.Registration;
using Myotragus.Data.RepositoryExtensions;
using Myotragus.Store.Model.Domain;
using Myotragus.Store.Model.Repositories;
using NUnit.Framework;

namespace Myotragus.Store.Model.Tests.StoreTests
{
  [TestFixture]
  public class StoreModelFixture
  {
    [SetUp]
    public void SetUp()
    {
      FixtureHelper.InitializeServices(ServicesImplementation.FromAssemblyContaining<IProduct>());
      var entitites = new[] { typeof(ICategory), typeof(IProduct) };
      FixtureHelper.InitializeEntities(entitites);
      FixtureHelper.InitializeSession(entitites);
    }

		[Test]
    public void InsertCategories()
    {
      var repo = FixtureHelper.Container.Resolve<ICategoryRepository>();
      var list = new List<ICategory>();

      for (var i = 0; i < 5; i++)
        list.Add(repo.New());

      foreach(var cul in FixtureHelper.LstCultureNames)
      {
        foreach(var category in list)
        {
          category.Name = FixtureHelper.GenerateSentence(1, cul);
          category.Description = FixtureHelper.GenerateParagraph(3, cul);
        }

        repo.Save(FixtureHelper.GetLcId(cul), list);
      }
    }

    public void InsertProducts()
    {
      var repo = FixtureHelper.Container.Resolve<IProductRepository>();
      var list = new List<IProduct>();

      for (var i = 0; i < 100; i++)
        list.Add(repo.New());

      foreach (var cul in FixtureHelper.LstCultureNames)
      {
        foreach (var category in list)
        {
          category.Name = FixtureHelper.GenerateSentence(1, cul);
          category.Description = FixtureHelper.GenerateParagraph(3, cul);
        }

        repo.Save(FixtureHelper.GetLcId(cul), list);
      }
    }

    public void ClearProducts()
    {
      var repo = FixtureHelper.Container.Resolve<IProductRepository>();

      var list = repo.FindAll(10);
      repo.Delete(list);

      list = repo.FindAll(10);
      Assert.AreEqual(0, list.Count());
      Assert.AreEqual(0, repo.CountAll());
    }

    [Test]
    public void ReadProducts()
    {
      var repo = FixtureHelper.Container.Resolve<IProductRepository>();

      foreach(var list in FixtureHelper.LstCultureNames
        .Select(cul => repo.FindAll(FixtureHelper.GetLcId(cul))))
      {
        Assert.AreEqual(10, list.Count());
        Assert.AreEqual(list.Count(), repo.CountAll());
        FixtureHelper.Print(list.First());
      }
    }

    [Test]
    public void UnExistingCulture()
    {
      var repo = FixtureHelper.Container.Resolve<IProductRepository>();

      var item = repo.FindAll(999).First();
      Assert.IsNull(item.Name);
      Assert.IsNull(item.Description);
    }

    [Test]
    public void Read()
    {
      var repo = FixtureHelper.Container.Resolve<ICategoryRepository>();

      foreach (var list in FixtureHelper.LstCultureNames
        .Select(cul => repo.FindAll(FixtureHelper.GetLcId(cul))))
      {
        FixtureHelper.Print(list.First());
      }
    }
  }
}
